# Contributor: Magnus Sandin <magnus.sandin@gmail.com>
# Maintainer: Magnus Sandin <magnus.sandin@gmail.com>
pkgname=tuxedo-drivers-src
_modname=tuxedo-drivers
pkgver=4.8.0
pkgrel=0
pkgdesc="Drivers for several platform devices for TUXEDO notebooks"
url="https://gitlab.com/tuxedocomputers/development/packages/tuxedo-drivers"
options="!check" # No tests available
arch="noarch"
license="GPL-3.0-or-later"
depends="akms"
makedepends="rsync"
install="$pkgname.post-deinstall"
source="https://gitlab.com/tuxedocomputers/development/packages/tuxedo-drivers/-/archive/v$pkgver/tuxedo-drivers-v$pkgver.tar.gz"
builddir="$srcdir/$_modname-v$pkgver"

package() {
	mkdir -p "$pkgdir"/usr/src/$_modname-$pkgver
	rsync --recursive --exclude=*.cmd --exclude=*.rules --exclude=*.d --exclude=*.ko --exclude=*.mod --exclude=*.mod.c --exclude=*.o --exclude=modules.order src/ "$pkgdir"/usr/src/$_modname-$pkgver

	cat > "$pkgdir"/usr/src/$_modname-$pkgver/AKMBUILD <<-EOF
	modname=$_modname
	modver=$pkgver-r$pkgrel
	built_modules="clevo_acpi.ko \
		clevo_wmi.ko \
		ite_8291/ite_8291.ko \
		ite_8291_lb/ite_8291_lb.ko \
		ite_8297/ite_8297.ko \
		ite_829x/ite_829x.ko \
		tuxedo_compatibility_check/tuxedo_compatibility_check.ko \
		tuxedo_io/tuxedo_io.ko \
		tuxedo_keyboard.ko \
		tuxedo_nb02_nvidia_power_ctrl/tuxedo_nb02_nvidia_power_ctrl.ko \
		tuxedo_nb04/tuxedo_nb04_kbd_backlight.ko \
		tuxedo_nb04/tuxedo_nb04_keyboard.ko \
		tuxedo_nb04/tuxedo_nb04_power_profiles.ko \
		tuxedo_nb04/tuxedo_nb04_sensors.ko \
		tuxedo_nb04/tuxedo_nb04_wmi_ab.ko \
		tuxedo_nb04/tuxedo_nb04_wmi_bs.ko \
		tuxedo_nb05/tuxedo_nb05_ec.ko \
		tuxedo_nb05/tuxedo_nb05_kbd_backlight.ko \
		tuxedo_nb05/tuxedo_nb05_keyboard.ko \
		tuxedo_nb05/tuxedo_nb05_power_profiles.ko \
		tuxedo_nb05/tuxedo_nb05_sensors.ko \
		uniwill_wmi.ko \
		"

	build() {
		make \$MAKEFLAGS -C "\$kernel_srcdir" M="\$builddir" src="\$srcdir"
	}
	EOF

	# Recursive Kbuild fails in an infinite loop, define all modules in one single Kbuild file instead
	find src/ -name Kbuild -exec sh -c 'd="$(dirname "$1" | cut -b 5-)" ; sed "s/.= /\+= $d\//g" < "$1" ; echo ""' shell {} \; | grep . | grep -v "obj-y " > "$pkgdir"/usr/src/$_modname-$pkgver/Kbuild
}

sha512sums="
7f81d6b07ea7955b1763020c659ac18dff36bbf04432deb2828618f14c151c0da8558fcc09b857ee34f92b95247692c7ec72e2c54b274d8e87ef68a8b6567b32  tuxedo-drivers-v4.8.0.tar.gz
"
